<?php
include '../class/class.detail.php';
include '../class/class.dokumen.php';


session_start();

$id_user=$_SESSION['id_user'];
$det=$_POST['id_det_usulan'];
$id_dok=$_POST['id_dok'];
$id_level=5;
$judul='Dokumen ke Setjen';
if($id_dok=='6'){
    $id_level=6;
    $judul='Dokumen persetujuan dari Setjen';
}

$details=explode(',',$det);

$error = "";
$msg = false;
$ekstensi = array('pdf', 'jpg', 'jpeg');
$direktori = 'dokumen';
$size = 2000000; //2mb
$nama_file='';
$detail=array();
$detailDao=array();
$hasil=array();
$mgs=array();
$err=array();
$dokumenDao=array();
$saveDok=array();

if(count($details)<=0){
    $error = "Pilih Pegawai Dahulu";
// }else if(empty($_FILES['file_proses']['tmp_name']) || $_FILES['file_proses']['tmp_name'] = 'none'){
//     $error = "File kosong ";
}else{
    
	$tmp = explode('.', $_FILES['file_proses']['name']);
    $fileEks = $tmp[count($tmp) - 1];
    $fileName = $tmp[0];
    $nama_file = "dokumen".$id_user."_".$_FILES['file_proses']['name'];

    if (in_array($fileEks, $ekstensi)) {
        if ($_FILES['file_proses']['size'] <= $size) {

            if (file_exists('../dokumen/' . $nama_file)) {
                $error = "File Sudah Ada, Silahkan Hapus File Dahulu";
            } else {
                
                $save = move_uploaded_file($_FILES['file_proses']['tmp_name'], '../' . $direktori . '/' . $nama_file);
                if(!$save){
                    $error="Gagal Simpan File";
                }else{
                    $j=0;
                    for($i=0;$i<count($details);$i++){
                        $detail[$i]=new Detail();

                        $detail[$i]->id_detail=$details[$i];
                        $detail[$i]->id_level=$id_level;
                        $detailDao[$i]=new DetailDao();
                        $hasil[$i]=$detailDao[$i]->prosesDetailDokumen($detail[$i]);
                        if($hasil[$i]){
                            $dokumenDao[$i]=new DokumenDao();
                            $saveDok[$i]=$dokumenDao[$i]->saveDokumenDetail($judul,$nama_file,$details[$i]);
                            if($saveDok[$i]){
                                $mgs[$i]=true;
                            }else{
                                $detail[$i]->id_detail=$details[$i];
                                $detail[$i]->id_level=5;
                                $detailDao[$i]=new DetailDao();
                                $hasil[$i]=$detailDao[$i]->prosesDetailDokumen($detail[$i]);
                                $mgs[$i]=false;
                                $err[$j]=$details[$i];
                                $j++;
                            }
                        }else{
                            $mgs[$i]=false;
                            $err[$j]=$details[$i];
                            $j++;
                        }
                    }
                    
					if(!in_array(false,$mgs)){
						$msg=true;
					}else{
                        $error="Upload Gagal";
                        for($x=0;$x<count($err);$x++){
                            if($x==0){
                                $error.=" untuk id ".$err[$x];
                            }else{
                                $error.=" ,".$err[$x];
                            }
                            
                        }   
					}				
                }
            }
        } else {
            $error = 'Maksimal Ukuran File 2mb';
        }
    } else {
        $error = 'Format File hanya PDF, JPG dan JPEG';
    }


    @unlink($_FILES['file_proses']);
}

$data=array(
    'msg' => $msg,
    'error' => $error,
    );

echo json_encode($data);

?>